package org.example.service;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.example.entity.Movie;
import org.example.entity.MovieType;
import org.example.entity.Ticket;
import org.example.util.DateUtil;

import java.io.*;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author TonySong
 * @date 2025/7/29 0029
 * @time 9:58
 */
public class CinemaService implements ICinemaService{
    @Override
    public Map<String,Movie> readXmlToMap() throws IOException, DocumentException, ParseException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(FILE);
        //获取第一层的所有子节点集合
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        for(Element element:elements){
            List<Element> list = element.elements();
            Movie movie =new Movie();
            for(Element e:list){
//                System.out.println(e.getName()+""+e.getText());
                String name =e.getName();

                switch (name){
                    case "name": movie.setName(e.getText());break;
                    case "direction": movie.setDirection(e.getText());break;
                    case "actor": movie.setActor(e.getText());break;
                    case "price": movie.setPrice(Integer.valueOf(e.getText()));break;
                    case "playTime":
                        movie.setPlayTime(DateUtil.getDateByString(e.getText()));
                        break;
                    case "type": movie.setType(MovieType.valueOf(e.getText()));break;
                }
                MOVIE_MAP.put(movie.getName(),movie);
            }
        }
        return MOVIE_MAP;
    }

    @Override
    public void showMovies() {
       if(MOVIE_MAP.size()>0){
           for(Movie movie:MOVIE_MAP.values()){
               System.out.println(movie);
           }
       }
    }

    @Override
    public Movie getMovieByName(String name) {
        if(MOVIE_MAP.containsKey(name)){
            return MOVIE_MAP.get(name);
        }
        return null;
    }

    @Override
    public void addTickets(Ticket ticket) {
        ICinemaService.SEAL_TICKETS.add(ticket);
        System.out.println("当前已销售"+ICinemaService.SEAL_TICKETS.size()+"张电影票。");
    }

    @Override
    public void saveTickets() throws IOException {
        if(ICinemaService.SEAL_TICKETS.size()>0) {
            ObjectOutputStream  w = new ObjectOutputStream(new FileOutputStream(ICinemaService.SEAL_TICKETS_FILE));
            //把销售集合进行序列化
            w.writeObject(ICinemaService.SEAL_TICKETS);
            w.close();
        }
    }

    @Override
    public List<Ticket> loadTickets() throws IOException, ClassNotFoundException {
        List<Ticket> tickets =null;
        if(ICinemaService.SEAL_TICKETS_FILE.exists()){
            ObjectInputStream in=new ObjectInputStream(new FileInputStream(ICinemaService.SEAL_TICKETS_FILE));
             tickets=(List<Ticket>) in.readObject();
            in.close();
            //把反序列化的集合的数据添加到当前的销售记录集合中
            ICinemaService.SEAL_TICKETS.addAll(tickets);
        }else{
            System.out.println("对比起，文件未找到，操作失败！");
        }
        return ICinemaService.SEAL_TICKETS;

    }

    @Override
    public void showSealTickets() {
        if(ICinemaService.SEAL_TICKETS.size()>0){
             ICinemaService.SEAL_TICKETS.forEach(ticket -> {
                 System.out.println(ticket);
             });
        }else{
            System.out.println("对不起，暂时没有销售记录。");
        }
    }
}
